<template>
</template>

<script>
	var _self;
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				codeType: '',
				codeVal: '',
			}
		},
		methods:{
			operateCode() {
                /*
				if(_self.codeType == 'payorder') {
					await _self.setLogin();
					uni.redirectTo({
					    url: '/merchant/payOrder?payCode='+_self.codeVal
					});
					return ;
				} else if (_self.codeType == 'sign') {
					await _self.setLogin();
					_self.signFun(_self.codeVal, 1);
				} else if (_self.codeType == 'recommendSign') {
					await _self.setLogin();
					_self.signFun(_self.codeVal, 2);
				} else if (_self.codeType == 'desk') {
					await _self.setLogin();
					uni.redirectTo({
					    url: '/lobbyEat/lobby?id='+_self.codeVal
					});
				} else if (_self.codeType == 'invitation') {
					uni.setStorageSync('recommend_mobile', _self.codeVal);
					uni.redirectTo({
					    url: '/pages/my/inviteRegister?recommend_mobile='+_self.codeVal
					});
				} else if (_self.codeType == 'bag') {
					uni.redirectTo({
					    url: '/merchant/bagDetails?id='+_self.codeVal
					});
				} else if (_self.codeType == 'product') {
					uni.redirectTo({
					    url: '/merchant/productDetail?id='+_self.codeVal
					});
				} else if (_self.codeType == 'recharge') {  //线下充值
					await _self.setLogin();
					uni.redirectTo({
					    url: '/pages/my/recharge?payCode='+_self.codeVal
					});
				} else if (_self.codeType == 'business') { //店铺入驻
                    let bArr = _self.codeVal.split('-');
					// uni.redirectTo({
					//     url: '/merchant/apply?type='+bArr[0]+'&bid='+bArr[1]
					// });
					uni.redirectTo({
					    url: '/business/apply?bid='+bArr[1]+'&applyType='+_self.codeType+bArr[0]
					});
				} else if (_self.codeType == 'salesman') { //申请业务员
					uni.redirectTo({
					    url: '/business/apply?bid='+_self.codeVal+'&applyType='+_self.codeType
					});
				} else if (_self.codeType == 'shop') { //超圳商行
					uni.setStorageSync('param_filter', '');
					uni.setStorageSync('param_filter', 'merchantId='+_self.codeVal); 
					uni.switchTab({
						url: '/pages/store/store'
					})
				} else if (_self.codeType == 'wholesaler') {  //批发商
					uni.redirectTo({
					    url: '/business/apply?bid='+_self.codeVal+'&applyType='+_self.codeType
					});
				} else if (_self.codeType == 'cz') {  //超圳商行
					uni.redirectTo({
					    url: '/business/promote_apply?mid='+_self.codeVal
					});
				} else if (_self.codeType == 'merchant') {  //店铺详情
					uni.redirectTo({
					    url: '/merchant/storeDetails?id='+_self.codeVal
					});
				} else if (_self.codeType == 'ebind') {  //店铺详情
					uni.redirectTo({
					    url: '/business/editBind?id='+_self.codeVal
					});
				} else if (_self.codeType == 'fv') {  //好友支付
					uni.redirectTo({
					    url: '/batch/view?field='+_self.codeVal
					});
				}*/
                
                if(_self.codeType == 'showLevel') {
                    if(uni.getStorageSync('access-token') != '') {
                        serve.request({
                        	url: '/member/member/show-level',
                        	data: {
                        		appkey: uni.getStorageSync('appkey'),
                                'access-token': uni.getStorageSync('access-token'),
                        		parentKey: _self.codeVal,
                        	},
                        	v: '/v3'
                        }).then(res => {
                        	if(res.code == 200) {
                                uni.redirectTo({
                                    url: '/pages/my/levelInfo'
                                });
                                return ;
                            }
                        });
                        return ;
                    }
                    uni.setStorageSync('parentKey', _self.codeVal);
                    uni.redirectTo({
                        url: '/pages/my/login'
                    });
                    return ;
                }
                if(_self.codeType == 'friends') {
                    if(uni.getStorageSync('access-token') != '') {
                        uni.switchTab({
                            url: '/pages/my/my'
                        });
                        return ;
                    }
                    uni.setStorageSync('parentKey', _self.codeVal);
                    uni.redirectTo({
                        url: '/pages/my/login'
                    });
                    return ;
                }
                if(_self.codeType == 'invitation') {
                    if(uni.getStorageSync('access-token') != '') {
                        uni.switchTab({
                            url: '/pages/my/my'
                        });
                        return ;
                    }
                    serve.request({
                    	url: '/site/invitation',
                    	data: {
                    		appkey: uni.getStorageSync('appkey'),
                    		codeVal: _self.codeVal,
                    	},
                    	v: '/v2'
                    }).then(res => {
                    	if(res.code == 200) {
                            uni.setStorageSync('parentKey', res.data.parentKey);
                            uni.redirectTo({
                                url: '/pages/my/login'
                            });
                            return ;
                        }
                    });
                    return ;
                }
                if(_self.codeType == 'applyRole') {
					var applyInfo = _self.codeVal.split('-');
					if(applyInfo.length != 2) {
						uni.showToast({
							title: '错误的二维码，请重新扫码',
							icon : 'none',
							success:function(){ 
								setTimeout(function () { 
									uni.switchTab({
									    url: '/pages/index/index'
									});
									return ;
								}, 2000) 
							} 
						});
						return ;
					}
                    uni.setStorageSync('parentKey', applyInfo[1]);
                    uni.redirectTo({
                        url: '/pages/store/apply?applyType='+applyInfo[0]+'&parentKey='+applyInfo[1]
                    });
                    return ;
                }
                if(_self.codeType == 'index') {  //邀请
                    uni.setStorageSync('parentKey', _self.codeVal);
                    uni.switchTab({
                        url: '/pages/index/index?'+'parentKey='+_self.codeVal
                    });
                    return ;
                }
				if(_self.codeType == 'order_pay') { //买单
					uni.redirectTo({
					    url: '/pages/store/billOrder?type=1&payCode='+_self.codeVal
					});
					return ;
				}
                if(_self.codeType == 'billPayCode') { //买单
                	uni.redirectTo({
                	    url: '/pages/store/billOrder?type=2&merchantCode='+_self.codeVal
                	});
                	return ;
                }
				if(_self.codeType == 'draw') {
					uni.redirectTo({
					    url: '/pages/common/draw?type='+_self.codeVal
					});
					return ;
				}
				if(_self.codeType == 'offline_recharge') { //线下充值
					uni.redirectTo({
					    url: '/merchant/offlineRecharge?id='+_self.codeVal
					});
					return ;
				}
				if(_self.codeType == 'online_recharge') { //商家特定充值
					uni.redirectTo({
					    url: '/merchant/onlineRecharge?rechargeId='+_self.codeVal
					});
					return ;
				}
                if(_self.codeType == 'onlineRecharge') { //商家特定充值
                	uni.redirectTo({
                	    url: '/merchant/fixedRecharge?merchantKey='+_self.codeVal
                	});
                	return ;
                }
				if(_self.codeType == 'bind') { //绑定店铺业务员
					uni.redirectTo({
					    url: '/pages/merchant/bind?mid='+_self.codeVal
					});
					return ;
				}
                if(_self.codeType == 'bindUser') { //绑定店铺业务员
                	uni.redirectTo({
                	    url: '/merchant/bindUser?merchantKey='+_self.codeVal
                	});
                	return ;
                }
                if (_self.codeType == 'applyMerchant') { //店铺入驻
                    console.info(_self.codeVal);
					uni.redirectTo({
					    url: '/merchant/apply?merchantKey='+_self.codeVal
					});
                    return 
				} 
                uni.showToast({
                	title: '错误的二维码，请重新扫码2',
                	icon : 'none',
                	success:function(){ 
                		setTimeout(function () { 
                			uni.switchTab({
                			    url: '/pages/index/index'
                			});
                			return ;
                		}, 2000) 
                	} 
                });
				return ;
			},
            checkFun() {
                serve.request({
                	url: '/site/check-code',
                	data: {
                		appkey: uni.getStorageSync('appkey'),
                		type: _self.codeType,
                	},
                    v: '/v3'
                }).then(res => {
                	if(res.code == 200) {
                        _self.operateCode();
                    } else {
                        uni.showToast({
                        	title: '错误的二维码，请重新扫码',
                        	icon : 'none',
                        	success:function(){ 
                        		setTimeout(function () { 
                        			uni.switchTab({
                        			    url: '/pages/index/index'
                        			});
                        			return ;
                        		}, 2000) 
                        	} 
                        });
                        return ;
                    }
                });
            }
		},
		onLoad(options) {
			_self = this;
			_self.codeType = '';
			_self.codeVal = '';
			if(options.q) {
                console.info(options.q);
				var link = decodeURIComponent(options.q);
				var paramStr = link.replace('https://hdsq.aoorange.cn/qrcode?', ""); 
				var dataItem = paramStr.split('=');
				if(dataItem.length != 2) {
					uni.showToast({
						title: '错误的二维码，请重新扫码1',
						icon : 'none',
						success:function(){ 
							setTimeout(function () { 
								uni.switchTab({
								    url: '/pages/index/index'
								});
								return ;
							}, 2000) 
						} 
					});
					return ;
				}
				_self.codeType = dataItem[0];
				_self.codeVal = dataItem[1];
			} 
			if(options.scene) {
				var dataItem = options.scene.split('-');
				_self.codeType = dataItem[0];
				_self.codeVal = dataItem[1];
			}
			
			
			if(_self.codeType == '' || _self.codeVal == ''){
				uni.showToast({
					title: '错误的访问来源',
					icon : 'none',
					success:function(){ 
						setTimeout(function () { 
							uni.switchTab({
							    url: '/pages/index/index'
							});
							return ;
						}, 2000) 
					} 
				});
			}
		},
        async onShow() {
            await this.$defaultLogin();
            _self.checkFun();
        }
	}
</script>

<style>
</style>
